package android.bluebox.view;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluebox.R;
import android.bluebox.model.StaticBox;
import android.bluebox.model.Tag;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class AValueTagListView extends Activity {

	ListView lvTag;
	ArrayList<Tag> tagListFull = new ArrayList<Tag>(); 
	ArrayList<String> tagListName = new ArrayList<String>();
	private int aValueId;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.avaluetaglist);

		aValueId = this.getIntent().getExtras().getInt("aValueId");
		lvTag = (ListView) findViewById(R.id.avt_list);
		lvTag.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		refreshList();
	}

	protected void onPause() {
		super.onPause();
		SparseBooleanArray checked = lvTag.getCheckedItemPositions();
		for (int i = 0; i < checked.size(); i++) {
			int tagId = tagListFull.get(i).getId();
			if (checked.get(i)) {
				StaticBox.dbBox.insertTagAValue(tagId, aValueId);
			} else {
				StaticBox.dbBox.deleteTagAValue(tagId, aValueId);
			}
		}
	}
	
	public void refreshList() {
		tagListFull = new ArrayList<Tag>(); 
		tagListName = new ArrayList<String>();
		loadTag();
		lvTag.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, 
				(String[])tagListName.toArray(new String[0])));// convert ArrayList to string[] http://appfulcrum.com/?p=281
		lvTag.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		loadCheckedItem();
	}

	public void loadTag() {
		StaticBox.dbBox.setContext(this);
		tagListFull = StaticBox.dbBox.getAllTags();
		for (Tag tag : tagListFull) {
			tagListName.add(tag.getName());
		}	
	}

	public void loadCheckedItem() {
		ArrayList<Tag> list = StaticBox.dbBox.getAValueTagList(aValueId);
		for (Tag tag : list) {
			if (tag == null) continue;
			int i = tagListName.indexOf(tag.getName());
			lvTag.setItemChecked(i, true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.avaluetaglist_optionsmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.avtl_new_tag:
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("New Identity");
			alert.setMessage("Name");

			// Set an EditText view to get user Input
			final EditText edtName = new EditText(this);
			alert.setView(edtName);

			alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {
					String newName = edtName.getText().toString().trim(); 
					if (newName.length() > 0) {
						createNewTag(newName);
					} else {
						Toast.makeText(getBaseContext(), "Please input tag name", Toast.LENGTH_SHORT).show();
					}
				}
			});

			alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {
					// TODO Auto-generated method stub
				}
			});
			alert.show();
			return true;
		default:
			return false; 
		}
	}

	public void createNewTag(String newName) {
		if (StaticBox.dbBox.getTag(newName) == null) {
			Tag tag = new Tag();
			tag.setName(newName);
			StaticBox.dbBox.insertTag(tag);
			refreshList();
		}
	}
}
